package com.snail.controller;

import com.alipay.api.AlipayApiException;
import com.snail.model.dto.PayRecordDto;
import com.snail.model.dto.QRCodeDto;
import com.snail.model.pojo.BizOrder;
import com.snail.model.vo.BizOrderVo;
import com.snail.response.BaseResponse;
import com.snail.service.BizOrderService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author Monica
 * @description
 * @date 2025/7/24 19:12
 */
@RestController
@RequestMapping("/order")
@AllArgsConstructor
public class BizOrderController {
    private final BizOrderService bizOrderService;


//    /**
//     * 创建二维码 (自动创建)
//     *
//     * @param id 订单id
//     */
//    @PostMapping("/createQRCode")
//    public BaseResponse<QRCodeDto> createQRCode(Integer id) {
//        return bizOrderService.createQRCode(id);
//
//    }

    /**
     * 创建订单
     *
     * @param bizOrderVo
     * @return
     */
    @PostMapping("/createOrder")
    public BaseResponse<QRCodeDto> createOrder(@RequestBody BizOrderVo bizOrderVo) {
        return bizOrderService.createOrder(bizOrderVo);
    }

    /**
     * 订单支付
     *
     * @param payNo        订单编号
     * @param httpResponse 响应对象
     * @throws IOException
     * @throws AlipayApiException
     */
    @GetMapping("/requestPay")
    public void requestPay(String payNo, Integer paymentMethod, HttpServletResponse httpResponse) throws IOException, AlipayApiException {
        bizOrderService.requestPay(payNo, paymentMethod, httpResponse);
    }

    /**
     * 查询订单支付结果
     *
     * @param payNo 订单编号
     * @return
     */
    @GetMapping("/payResult")
    @ResponseBody
    public BaseResponse<PayRecordDto> payResult(String payNo) throws IOException {
        //查询支付结果
        return bizOrderService.queryPayResult(payNo);

    }

    /**
     * 订单支付通知
     *
     * @param request
     * @return
     */
    @PostMapping("/payNotify")
    public void payNotify(HttpServletRequest request, HttpServletResponse response) throws AlipayApiException {
        bizOrderService.payNotify(request, response);
    }

    /**
     * 查看所有的订单列表
     */
    @GetMapping("list")
    public BaseResponse<List<BizOrder>> findAllOrders(Integer userId) {
        return bizOrderService.findAllOrders(userId);
    }
}


 

  


